package com.school.demo.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.school.demo.model.auto.Account;
import com.school.demo.model.auto.Organization;
import com.school.demo.model.auto.Student;
import com.school.demo.model.auto.User;
import com.school.demo.service.IOrganizationService;
import com.school.demo.service.IStudentService;
import com.school.demo.service.IUserService;
import com.school.demo.util.GsonUtil;
import com.school.demo.util.Restful;
import com.school.demo.util.Status;
import com.school.demo.util.UserUitl;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: demo
 * @description: Android端登录api接口
 * @author: Charlotte
 * @create: 2021-03-13 17:57
 **/

@Api(tags = "用户登录API")
@RestController
@RequestMapping("/api/user")
public class LoginAPIController {


    @Autowired
    IUserService userService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IOrganizationService organizationService;

    @ApiOperation(value = "用户登录", notes = "", tags = {})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    Restful login(@RequestParam("role") int role,@RequestParam("userName") String userName,@RequestParam("password") String password) throws Exception {

        switch (role){
            case 1:
                //学生
                //验证用户名是否合法
                if (UserUitl.isPhone(userName) || UserUitl.isEmail(userName)){
                    String log = "学生用户"+userName+"匹配成功，正在登录...";
                    System.out.println(log);
                }else{
                    String log = "学生用户"+userName+"匹配失败！";
                    System.out.println(log);
                    return new Restful(Status.LOGIN_ERROR);
                }

                //验证用户是否存在
                QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Student::getPhone,userName);
                Student student=studentService.getOne(queryWrapper);

                if (student == null || !student.getPassword().equals(password)){
                    return new Restful(Status.LOGIN_ERROR);
                }

                Restful restful = new Restful(Status.SUCCESS);
                restful.setData(student);
                return restful;
            case 0:
                //组织
                //验证用户名是否合法
                if (UserUitl.isPhone(userName) || UserUitl.isEmail(userName)){
                    String log = "组织用户"+userName+"匹配成功，正在登录...";
                    System.out.println(log);
                }else{
                    String log = "组织用户"+userName+"匹配失败！";
                    System.out.println(log);
                    return new Restful(Status.LOGIN_ERROR);
                }

                //验证用户是否存在
                QueryWrapper<Organization> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(Organization::getPhone,userName);
                Organization organization=organizationService.getOne(queryWrapper1);

                if (organization == null || !organization.getPassword().equals(password)){
                    return new Restful(Status.LOGIN_ERROR);
                }

                Restful restful1 = new Restful(Status.SUCCESS);
                restful1.setData(organization);
                return restful1;
            default:
                throw new IllegalStateException("Unexpected value: " + role);
        }


    }


    @GetMapping("/hello")
    public String hello(){
        return "dsaf ";
    }


    @ApiOperation(value = "退出登录", notes = "", tags = {})
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public @ResponseBody
    Restful logout(@RequestBody String json) throws Exception {

        return new Restful(Status.SUCCESS);

    }


    @ApiOperation(value = "注册", notes = "", tags = {})
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public @ResponseBody
    Restful register(@RequestBody Student student) throws Exception {

        Student student1 = studentService.getByPhone(student.getPhone());
        if (null == student1){
            return new Restful(Status.LOGIN_USER_EXIST);
        }
        Map<String,Object> map = new HashMap<>();
        map = studentService.register(student);
        return new Restful(Status.SUCCESS);

    }

}
